package com.seal.yoyoshop.dao.impl;

import com.seal.yoyoshop.bin.Goods;
import com.seal.yoyoshop.dao.GoodsDao;
import com.seal.yoyoshop.util.DBUtils;

import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {

    @Override
    public List<Goods> selectGoodsByHotType(int type) {
        String sql = "SELECT g.* FROM top AS t JOIN goods AS g ON t.good_id = g.id WHERE type = ?";
        List<Goods> goodsList = DBUtils.query(sql, Goods.class, type);
        return goodsList;
    }

    @Override
    public List<Goods> selectGoodsByTypeId(int typeId) {
        String sql = "SELECT * FROM goods WHERE type_id = ? AND status = 1";
        List<Goods> goodsList = DBUtils.query(sql, Goods.class, typeId);
        return goodsList;
    }

    @Override
    public List<Goods> selectGoodsByTypeId(int typeId, int page, int size) {
        String sql = "SELECT * FROM goods WHERE type_id = ? AND status = 1 LIMIT ?,?";
        List<Goods> goodsList = DBUtils.query(sql, Goods.class, typeId,(page-1)*size,size);
        return goodsList;
    }

    @Override
    public long countByTypeId(int typeId) {
        String sql = "SELECT count(*) AS count FROM goods WHERE type_id = ? AND status = 1";
        Map<String, Object> result = DBUtils.queryOne(sql,typeId);
        long total = (long) result.get("count");
        return total;
    }

    @Override
    public long countByHotType(int typeId) {
        String sql = "SELECT count(*) AS count FROM top AS t JOIN goods AS g ON t.good_id = g.id WHERE type = ? AND g.status = 1";
        Map<String, Object> result = DBUtils.queryOne(sql,typeId);
        long total = (long) result.get("count");
        return total;
    }

    @Override
    public List<Goods> selectGoodsByHotType(int type, int page, int size) {
        String sql = "SELECT g.* FROM top AS t JOIN goods AS g ON t.good_id = g.id WHERE type = ? LIMIT ?,?";
        List<Goods> goodsList = DBUtils.query(sql, Goods.class, type,(page-1)*size,size);
        return goodsList;
    }

    @Override
    public List<Goods> selectGoodsByKeyWord(String keyword, int page, int size) {
        String sql = "SELECT * FROM goods WHERE name LIKE '%" + keyword + "%' AND status = 1 LIMIT ?,?";
        List<Goods> goodsList = DBUtils.query(sql, Goods.class, (page-1)*size,size);
        return goodsList;
    }

    @Override
    public long countByKeyword(String keyword) {
        String sql = "SELECT count(*) AS count FROM goods WHERE name LIKE '%" + keyword + "%' AND status = 1";
        Map<String, Object> result = DBUtils.queryOne(sql);
        long total = (long) result.get("count");
        return total;
    }

    @Override
    public Goods selectGoodById(int goodId) {
        String sql = "SELECT * FROM goods WHERE id = ?";
        Goods good = DBUtils.queryOne(sql,Goods.class,goodId);
        return good;
    }

    @Override
    public List<Goods> selectGoodsList(Integer page, Integer size) {
        String sql = "SELECT * FROM goods WHERE status = 1 LIMIT ?,?";
        List<Goods> goodsList = DBUtils.query(sql,Goods.class,(page-1) * size,size);
        return goodsList;
    }

    @Override
    public long selectGoodsCount() {
        String sql = "SELECT count(*) AS count FROM goods WHERE status = 1";
        Map<String,Object> map = DBUtils.queryOne(sql);
        long count = (long)map.get("count");
        return count;
    }

    @Override
    public boolean addGoodsByGoods(Goods goods) {
        String sql = "INSERT INTO goods(name, price, intro, cover, image1, image2, stock, type_id, status) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
        int row = DBUtils.update(sql,goods.getName(), goods.getPrice(), goods.getIntro(), goods.getCover(), goods.getImage1(), goods.getImage2(), goods.getStock(), goods.getTypeId(), goods.getStatus());
        if(row == 1)return true;
        return false;
    }

    @Override
    public boolean addGoodsByGoods(int goodsId) {
        String sql = "UPDATE goods SET status = 0 WHERE id = ?";
        int row = DBUtils.update(sql,goodsId);
        if(row == 1)return true;
        return false;
    }

    @Override
    public boolean updateGoodsByGoods(Goods goods) {
        String sql = "UPDATE goods SET name = ?, price = ? , intro = ? , cover = ? , image1 = ?, image2 = ?, stock = ?, type_id = ? WHERE id = ?";
        int row = DBUtils.update(sql,goods.getName(), goods.getPrice(), goods.getIntro(), goods.getCover(), goods.getImage1(), goods.getImage2(), goods.getStock(), goods.getTypeId(),goods.getId());
        if(row == 1)return true;
        return false;
    }

}
